<?php
/**
 * 短信猫客户端操作API
 * @author 天酷 QQ:593244214
 * @copyright 天酷网络&copy;2011 http://bbs.8eoo.com
 */
class MaoAction extends Action{
    var $result;
    function init(){
		$auth =  isset($_SERVER['HTTP_CLIENT_AUTH_CODE'])?$_SERVER['HTTP_CLIENT_AUTH_CODE']:null;
		$auth_code = SiteConfig::get('auth_code');
		if(!$auth || md5($auth) != $auth_code){
			echo json_encode(array('MSG'=>'客户端验证失败','STS'=>false,'RT'=>null));
			exit; 
		}
    }

    function cmdIndex(){
        header('Content-Type:text/html');
        exit("抱歉！你访问的页面不存在");
    }
    
    function cmdcheckAuth(){
    	echo json_encode(array('MSG'=>'auth success','STS'=>true,'RT'=>null));
    }

    /**
     * 接收客户端发来的短信并送出
     */
    function cmdApiJson(){
        $mode=empty($_SERVER['HTTP_MODE'])?"SMS":$_SERVER['HTTP_MODE'];
        $phonenum=isset($_REQUEST['phone'])?$_REQUEST['phone']:null;
        $message=isset($_REQUEST['message'])?$_REQUEST['message']:null;
        $num=isset($_REQUEST['num'])?$_REQUEST['num']:'0';
        $num=intval($num);
        
        if(empty($phonenum) || empty($message)){
        	echo json_encode(array('MSG'=>'参数错误','STS'=>false,'RT'=>null)); 
        }else{
        	$mao=new MaoModel();
        	$phonenum=preg_replace('@\+\d\d@is', "", $phonenum);
        	if(stripos($message,"%")!=-1)$message=urldecode($message);
        	$message=preg_replace('/\+*\+|\|*\||＃|﹟|｜|　| * /is', "#", $message);
        	echo $mao->PostJSON(qstr($phonenum),qstr($message),qstr($mode));
        }
    }

    /**
     * 客服端获取指令JSON
     */
    function cmdgetCommandJson(){
            $mao=new MaoModel();
            $com=$mao->getMaoList(null,1,null);
            if(!$com['data']){
                echo json_encode(array('STS'=>FALSE,'MSG'=>'用户没有指令'));
                return;
            }else{
                foreach ($com['data'] as $key=>$value) {
                    $a[]=$value['command_content'];
                }
                echo json_encode(array('STS'=>true,'MSG'=>join('|', $a)));
            }
    }
   
}
